﻿#include "mpConfig.h"
#include "mpLog.h"
#include "mpFileUtils.h"



namespace MP{
namespace Log{

////private member
//namespace{
//} //private member


/**
 * @brief 初始化日志组件
 * @param appName 应用程序名称
 * @param appName 应用程序日志目录
 */
void InitLog(const std::string & appName,
             const std::string & logWorkDir){

    if(!MP::FileUtils::check_dir_isexist(logWorkDir)){
        MP::FileUtils::create_directories(logWorkDir);
    }

    //设置日志大小滚动
    el::Loggers::addFlag(el::LoggingFlag::StrictLogFileSizeCheck);
    //从配置文件中读取日志配置
    std::string file = MP::FileUtils::get_appdir() + PATH_DELIM + "myEasylogging.conf";

    if(MP::FileUtils::check_file_isexist(file)){
        el::Configurations conf(file);
        //设置日志文件路径
        conf.set(el::Level::Global,
                 el::ConfigurationType::Filename, logWorkDir + PATH_DELIM + "mp_" + appName + "_log_%datetime{%Y%M}.log");

        el::Loggers::reconfigureAllLoggers(conf);
    }else{
        el::Configurations defaultConf;
        //defaultConf.setToDefault();
        //重新设置日志格式
        defaultConf.set(el::Level::Global,
                        el::ConfigurationType::Format, "[%level | %datetime] | %msg");
        //重新设置日志文件路径
        defaultConf.set(el::Level::Global,
                        el::ConfigurationType::Filename, logWorkDir + PATH_DELIM + "mp_" + appName + "_log_%datetime{%Y%M}.log");
        //重新设置日志文件大小(5M)
        defaultConf.set(el::Level::Global,
                        el::ConfigurationType::MaxLogFileSize, "5242880");

        el::Loggers::reconfigureAllLoggers(defaultConf);
    }

    MPLOG_INFO << "#############################";
    MPLOG_INFO << "The log component is initialized and the application is starting ...";
}

}   //END Log
}   //END MP


//设置日志所在目录（PS：如果是NT服务SYSTEM用户启动的程序，那么则日志直接存储与应用程序所在目录中，否则存储在AppDataDirectory中）

//std::string logWorkDir = "";
//#if MAPLE_RUNAS_SVR
//logWorkDir = MP::FileUtils::getAllUserAppDataDirectory() + PATH_DELIM + "logs";
//#endif //MAPLE_RUNAS_SVR

//if(appName == "mpCmd"){
//    logWorkDir = MP::FileUtils::get_all_userdata_dir() + PATH_DELIM + "logs_cmd";
//}

//if(logWorkDir.empty()){
//    logWorkDir = MP::FileUtils::get_current_userdata_dir() + PATH_DELIM + "logs";
//}



